package com.platform.module.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.constant.UserConstants;
import com.platform.common.core.text.Convert;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.DateUtils;
import com.platform.common.utils.StringUtils;
import com.platform.module.domain.BaseRoomTemp;
import com.platform.module.mapper.BaseFactoryMapper;
import com.platform.module.mapper.BaseRoomTempMapper;
import com.platform.module.service.IBaseRoomTempService;
import com.platform.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 室温采集器资料Service业务层处理
 * 
 * @author wangqi
 * @date 2020-07-28
 */
@Service
public class BaseRoomTempServiceImpl extends ServiceImpl<BaseRoomTempMapper,BaseRoomTemp> implements IBaseRoomTempService
{
    @Autowired
    private BaseRoomTempMapper baseRoomTempMapper;

    /**
     * 查询室温采集器资料列表
     * 
     * @param baseRoomTemp 室温采集器资料
     * @return 室温采集器资料
     */
    @Override
    public List<BaseRoomTemp> selectBaseRoomTempList(BaseRoomTemp baseRoomTemp)
    {
        return baseRoomTempMapper.selectBaseRoomTempList(baseRoomTemp);
    }

    /**
     * 新增室温采集器资料
     * 
     * @param baseRoomTemp 室温采集器资料
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBaseRoomTemp(BaseRoomTemp baseRoomTemp)
    {
        baseRoomTemp.setCreateTime(DateUtils.getNowDate());
        return baseRoomTempMapper.insert(baseRoomTemp);
    }

    /**
     * 修改室温采集器资料
     * 
     * @param baseRoomTemp 室温采集器资料
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBaseRoomTemp(BaseRoomTemp baseRoomTemp)
    {
        baseRoomTemp.setUpdateTime(DateUtils.getNowDate());
        return baseRoomTempMapper.updateById(baseRoomTemp);
    }

    /**
     * 删除室温采集器资料对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBaseRoomTempByIds(String ids)
    {
        return baseRoomTempMapper.deleteBatchIds(Arrays.asList(Convert.toStrArray(ids)));
    }

    /**
     * 删除室温采集器资料信息
     * 
     * @param baseId 室温采集器资料ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBaseRoomTempById(Long baseId)
    {
        return baseRoomTempMapper.deleteById(baseId);
    }

    /**
     * 导入室温采集器信息数据
     *
     * @param roomTempList 室温采集器信息数据
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    @Transactional
    public String importBaseRoomTemp(List<BaseRoomTemp> roomTempList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(roomTempList) || roomTempList.size() == 0)
        {
            throw new BusinessException("导入室温采集器信息数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (BaseRoomTemp roomTemp : roomTempList)
        {
            try
            {
                //查询室温采集器信息是否存在
                QueryWrapper<BaseRoomTemp> dataQueryWrapper = new QueryWrapper<BaseRoomTemp>();
                dataQueryWrapper.eq("brt_id",roomTemp.getBrtId());
                BaseRoomTemp room = baseRoomTempMapper.selectOne(dataQueryWrapper);
                if (StringUtils.isNull(room))
                {
                    roomTemp.setCreateBy(operName);
                    roomTemp.setCreateTime(DateUtils.getNowDate());
                    this.insertBaseRoomTemp(roomTemp);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、室温采集器 " + roomTemp.getBrtId() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    roomTemp.setUpdateBy(operName);
                    roomTemp.setUpdateTime(DateUtils.getNowDate());
                    this.updateBaseRoomTemp(roomTemp);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、室温采集器 " + roomTemp.getBrtId() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、室温采集器 " + roomTemp.getBrtId() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、室温采集器 " + roomTemp.getBrtId() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 校验厂商代码是否唯一
     *
     * @param baseRoomTemp 厂商信息
     * @return 结果
     */
    @Override
    public String checkRoomTempCodeUnique(BaseRoomTemp baseRoomTemp)
    {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("brt_id",baseRoomTemp.getBrtName());
        BaseRoomTemp roomTemp = baseRoomTempMapper.selectOne(queryWrapper);
        if (StringUtils.isNotNull(roomTemp) )
        {
            return UserConstants.TEMP_NAME_NOT_UNIQUE;
        }
        return UserConstants.TEMP_NAME_UNIQUE;
    }
    /**
     * 根据条件分页查询已分配室温采集器列表
     *
     * @param roomTemp 室温采集器信息
     * @return 室温采集器信息集合信息
     */
    @Override

    public List<BaseRoomTemp> selectAllocatedSubList(BaseRoomTemp roomTemp)
    {
        return baseRoomTempMapper.selectAllocatedSubList(roomTemp);
    }


    /**
     * 根据条件分页查询未分配室温采集器角色列表
     *
     * @param baseRoomTemp 室温采集器信息
     * @return 室温采集器信息集合信息
     */
    @Override

    public List<BaseRoomTemp> selectUnallocatedSubList(BaseRoomTemp baseRoomTemp)
    {
        return baseRoomTempMapper.selectUnallocatedSubList(baseRoomTemp);
    }


}
